package com.dongzili.demo.customer.sync.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface CustomerMapper<T> extends com.baomidou.mybatisplus.core.mapper.BaseMapper<T> {

//    @Select("select ${columns} from ${tableName}")
//    Page<E> selectListPage(Page<T> page, @Param("tableName") String tableName, @Param("columns") String columns);
//
//    @Select("select ${columns} from ${tableName}")
//    List<Map<String, Object>> selectListMap(Page<T> page, @Param("tableName") String tableName, @Param("columns") String columns);

    /**
     * 自定义分页查询
     * @param tableName
     * @param columns
     * @param pageStart
     * @param pageEnd
     * @return
     */
//    @Select("select ${columns} from ${tableName} limit ${pageStart},${pageEnd} ")
    @Select("select ${columns} from ${tableName} where ${primaryKey} >= ( select ${primaryKey} from ${tableName} order by ${primaryKey} limit ${pageStart},1 ) limit ${pageEnd}")
    List<Map<String, Object>> selectListMapByPage(@Param("tableName") String tableName, @Param("columns") String columns, @Param("primaryKey") String primaryKey, @Param("pageStart") Integer pageStart, @Param("pageEnd") Integer pageEnd);

    /**
     * 查询n天内新增或有更新的数据
     * @param tableName
     * @param columns
     * @param primaryKey
     * @param day
     * @param pageStart
     * @param pageEnd
     * @return
     */
    @Select("select ${columns} from ${tableName} where modify_time >= NOW() - INTERVAL ${day} DAY and ${primaryKey} >= ( select ${primaryKey} from ${tableName} order by ${primaryKey} limit ${pageStart},1 ) limit ${pageEnd}")
    List<Map<String, Object>> selectListMapByPageAndDay(@Param("tableName") String tableName, @Param("columns") String columns, @Param("primaryKey") String primaryKey, @Param("day") int day, @Param("pageStart") Integer pageStart, @Param("pageEnd") Integer pageEnd);

//    @Select("select ${columns} from ${tableName} where modify_time >= NOW() - INTERVAL ${day} DAY and ${primaryKey} >= ( select ${primaryKey} from ${tableName} order by ${primaryKey} limit ${pageStart},1 ) limit ${pageEnd}")
//    List<Map<String, Object>> selectListMapByPageAndDay(@Param("tableName") String tableName, @Param("columns") String columns, @Param("primaryKey") String primaryKey, @Param("day") int day, @Param("pageStart") Integer pageStart, @Param("pageEnd") Integer pageEnd);

    /**
     * 根据id查询
     * @param tableName
     * @param columns
     * @param primaryKey
     * @param id
     * @return
     */
    @Select("select ${columns} from ${tableName} where ${primaryKey} = ${id}")
    Map<String, Object> selectOneById(@Param("tableName") String tableName, @Param("columns") String columns, @Param("primaryKey") String primaryKey, @Param("id") Object id);


    /**
     * 查询总数
     * @param tableName
     * @return
     */
    @Select("select count(1) from ${tableName}")
    int selectCount(@Param("tableName") String tableName);

    @Select("select count(1) from ${tableName} where modify_time >= NOW() - INTERVAL ${day} DAY")
    int selectCountByRecent(@Param("tableName") String tableName, @Param("day") int day);

    @Select("select ${columns} from ${tableName}")
    List<Map<String, Object>> selectListObj(Page<T> page, @Param("tableName") String tableName, @Param("columns") String columns);

    @Select("select ${columns} from ${tableName} order by ${primaryKey} desc limit 1")
    String selectOne(@Param("tableName") String tableName, @Param("columns") String columns, @Param("primaryKey") String primaryKey);
}
